package com.sbck.cs3.fragment;

import android.graphics.Color;
import android.os.Bundle;

import android.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.sbck.cs3.MyClass.MessageType;
import com.sbck.cs3.MyGlobal;
import com.sbck.cs3.MyClass.MyMessage;
import com.sbck.cs3.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class UserCtrlFragment extends Fragment {

    private Button buttonHeart;
    private Button buttonPower;
    private Button buttonAlarmStop;
    private Button buttonXCWM;
    private Button buttonXCCY;
    private Button buttonRGZY;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user_ctrl, container, false);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        buttonHeart = view.findViewById(R.id.Btn_Ctrl_Heart);
        buttonAlarmStop = view.findViewById(R.id.Btn_Ctrl_AlarmStop);
        buttonPower = view.findViewById(R.id.Btn_Ctrl_Power);
        buttonXCCY = view.findViewById(R.id.Btn_Ctrl_XCCY);
        buttonXCWM = view.findViewById(R.id.Btn_Ctrl_XCWM);
        buttonRGZY = view.findViewById(R.id.Btn_Ctrl_RGZY);

        //EventBus
        if(!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }

        MyGlobal.HeartCount = 0;

        MyGlobal.socketUtil.GetHeart();

        SetButtonDisable();

        ButtonClick();
    }
    //按钮事件
    private void ButtonClick() {

        //获取心跳按钮
        buttonHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ShowToast("下发获取设备心跳信息指令");
                MyGlobal.socketUtil.GetHeart();
            }
        });
        //电源按钮
        buttonPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(MyGlobal.unitstatus.SDZT)
                {
                    if(IsCanSendCloseCmd(MessageType.SysPower))
                    {
                        String msg = "下发关闭系统电源指令...";
                        ShowToast(msg);
                        MyGlobal.socketUtil.SendCmd_SYSPower(false,MyGlobal.unitstatus.SDZT);
                        MyGlobal.AddLog(msg,Color.DKGRAY);
                    }
                }
                else {
                    if(IsCanSendOpenCmd(MessageType.SysPower))
                    {
                        String msg = "下发打开系统电源指令...";
                        ShowToast(msg);
                        MyGlobal.socketUtil.SendCmd_SYSPower(false,MyGlobal.unitstatus.SDZT);
                        MyGlobal.AddLog(msg,Color.DKGRAY);
                    }
                }
            }
        });

        //急停
        buttonAlarmStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MyGlobal.socketUtil.SendCmd_AlarmStop();
                if (MyGlobal.unitstatus.AlarmStop)
                {
                    String msg = "下发紧急停机[复位]指令...";
                    Toast.makeText(getActivity(),"下发紧急停机[复位]指令!" ,Toast.LENGTH_SHORT).show();
                    MyGlobal.AddLog(msg,Color.DKGRAY);
                }
                else
                {
                    String msg = "下发紧急停机[关闭电源]指令...";
                    ShowToast(msg);
                    Toast.makeText(getActivity(),"下发紧急停机[关闭电源]指令!" ,Toast.LENGTH_SHORT).show();
                    MyGlobal.AddLog(msg,Color.DKGRAY);
                }

            }
        });


    }


    private void ShowToast(String msg)
    {
        if(Boolean.TRUE.equals(MyGlobal.IsPopToast.get()))
            Toast.makeText(getActivity(),msg , Toast.LENGTH_SHORT).show();
    }

    private boolean IsCanSendOpenCmd(MessageType messageType)
    {
        if(MyGlobal.HeartCount <= 0)
        {
            return ShowErrMsg(MessageType.NoHeart,"等待心跳中");
        }

        switch (messageType) {
            case None:
            case Heart:
                break;
            case SysPower:
            case WorkModeXCCY:
            case WorkModeXCWM:
            case WorkModeRGZY:
                if(MyGlobal.unitstatus.AlarmStop)
                    return ShowErrMsg(messageType,"急停状态");
                if(MyGlobal.unitstatus.FJPL > 0)
                    return ShowErrMsg(messageType,"风机运行中,请先关闭风机!");
                if(MyGlobal.unitstatus.YTWorkTime > 0)
                    return ShowErrMsg(messageType,"人工增雨工作中");
                if(MyGlobal.unitstatus.JRWorkTime > 0)
                    return ShowErrMsg(messageType,"除霾工作中");
                if(MyGlobal.unitstatus.O3WorkTime > 0)
                    return ShowErrMsg(messageType,"除臭氧工作中");

            case Error:
                break;

        }

        return true;
    }
    private boolean IsCanSendCloseCmd(MessageType messageType)
    {
        if(MyGlobal.HeartCount <= 0)
        {
            return ShowErrMsg(MessageType.NoHeart,"等待心跳中");
        }
        switch (messageType) {
            case None:
            case Heart:
                break;
            case SysPower:
                if(MyGlobal.unitstatus.AlarmStop )
                    return ShowErrMsg(messageType,"急停状态");
                break;
            case FWJ://方位角
            case AlarmStop:
            case YTFan:
            case Error:
                break;
        }

        return true;
    }

    private boolean ShowErrMsg(MessageType messageType,String msg)
    {
        String Msg = "[ " + messageType.getName() + " ]命令错误:"+msg;

        Toast.makeText(getActivity(),Msg,Toast.LENGTH_SHORT).show();
        MyGlobal.AddLog(Msg,Color.RED);
        return false;
    }



    //事件总线
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MyMessage myMessage) {

        switch (myMessage.messageCategory){

            case SocketReceive:
                switch (myMessage.messageType) {
                    case Heart:
                        SetButtonStatus();
                        break;
                    case SysPower:
                        //上电
                        SetButtonColor(buttonPower,MyGlobal.unitstatus.SDZT);
                        break;
                    case AlarmStop:
                        //急停
                        if(MyGlobal.unitstatus.AlarmStop)
                        {
                            buttonAlarmStop.setText("复  位");
                            buttonAlarmStop.setBackgroundColor(Color.RED);
                        }
                        else
                        {
                            buttonAlarmStop.setText("急  停");
                            buttonAlarmStop.setBackgroundColor(Color.RED);
                        }
                        break;
                }
            case SocketSend:
                break;
            case SocketError:
                if(myMessage.messageType == MessageType.HeartTimeout)
                {
                    SetButtonDisable();
                    return;
                }
        }

    }

    private void SetButtonColor(Button button, boolean status)
    {
        if(status)
            button.setBackgroundColor(Color.GREEN);
        else
            button.setBackgroundColor(Color.DKGRAY);
    }

    private void SetButtonDisable()
    {
        buttonPower.setBackgroundColor(Color.GRAY);
        buttonRGZY.setBackgroundColor(Color.GRAY);
        buttonXCWM.setBackgroundColor(Color.GRAY);
        buttonXCCY.setBackgroundColor(Color.GRAY);
    }

    private void SetButtonStatus()
    {
        //上电
        SetButtonColor(buttonPower,MyGlobal.unitstatus.SDZT);

        //急停
        if(MyGlobal.unitstatus.AlarmStop)
        {
            buttonAlarmStop.setText("复  位");
            buttonAlarmStop.setBackgroundColor(Color.RED);
        }
        else
        {
            buttonAlarmStop.setText("急  停");
            buttonAlarmStop.setBackgroundColor(Color.RED);
        }

    }
}